#ifndef ONEGINFUNCTIONS_H
#define ONEGINFUNCTIONS_H

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <MyFunctions.h>

void Sorting_The_Text(char **Buffer, char **text,int counter) //sorting pointers to the lines
{
    int Counter_Of_Lines = 0,Counter_For_Smaller_Lines = 0;
    for (Counter_Of_Lines = 0; Counter_Of_Lines < counter; Counter_Of_Lines++)//Bubble sorting the functions
    for (Counter_For_Smaller_Lines = Counter_Of_Lines + 1; Counter_For_Smaller_Lines <= counter; Counter_For_Smaller_Lines++)
        if (strcmp(text[Counter_Of_Lines], text[Counter_For_Smaller_Lines]) >= 0)
        {
            Buffer = text[Counter_Of_Lines];
            text[Counter_Of_Lines] = text[Counter_For_Smaller_Lines];
            text[Counter_For_Smaller_Lines] = Buffer;
        }
}
//---------------------------------------------------------------------------------

void Writing_to_file(char **Final_Text, int counter, FILE *File_To_Write_To,char *name)
{
    File_To_Write_To = fopen(name,"w");
    Checking_File(File_To_Write_To, name);
    int i = 0;
    for (i = 0; i <= counter; i++)
    {
        fprintf(File_To_Write_To,"\n%s",Final_Text[i]);
        printf("\n%s",Final_Text[i]);
    }
    fclose(File_To_Write_To);
}

//---------------------------------------------------------------------------------

int Reading_From_File(char* Buffer,char** text,FILE* File_To_Read_From,char* name)                    //read Onegin, pointers in text
{
    int i = 0, NumOfLines = 0;                                                     //variables for loops
    char Letter_In_Parent_Str;                                                  //The last symbol
    int Code_Of_The_Letter_In_Parent_Str;                                       //variable that equals to c,created to avoid 'ß'='EOF' problem
    File_To_Read_From = fopen(name,"r");
    Checking_File(File_To_Read_From,name);
    while((Code_Of_The_Letter_In_Parent_Str=getc(File_To_Read_From))!=EOF)
    {
        Letter_In_Parent_Str = Code_Of_The_Letter_In_Parent_Str;
        Buffer[i] = Letter_In_Parent_Str;
        i++;

        if(Letter_In_Parent_Str == '\n')                                        //if line ends then another pointer is used
        {
            Buffer[i-1] = 0;
            NumOfLines++;
            text[NumOfLines] = (char*) calloc(1,sizeof(char));
            text[NumOfLines] = &Buffer[i];
        }

    }
    fclose(File_To_Read_From);
    return NumOfLines;
}
//---------------------------------------------------------------------------------



#endif // ONEGINFUNCTIONS_H
